package DealCard;

import Objects.Card;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class DealCard extends javax.swing.JFrame {

    // Card array
    ArrayList<JLabel> arrJLabel = new ArrayList<>();
    JLabel lbl;
    Card c = new Card();
    //--
    // Size of the card
    int iCardWidth = c.getiCardWidth(), iCardHeight = c.getiCardHeight();
    //--
    // Size of the monitor
    Dimension Size = Toolkit.getDefaultToolkit().getScreenSize();
    int iWidth = Size.width, iHeight = Size.height - 20;
    int iBot = 100;
    int iTop = iHeight / 15;
    int iLeft = iWidth / 15;
    int iRight = iWidth / 15;
    //--
    // Get center
    int iCenterX = iWidth / 2 - iCardWidth / 2;
    int iCenterY = iHeight / 2 - iCardHeight / 2;
    //-- End Get center

    public DealCard() {
        initComponents();
        this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        makeArrayCard();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnDeal = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(1000, 600));
        setMinimumSize(new java.awt.Dimension(1000, 600));
        setPreferredSize(new java.awt.Dimension(1000, 600));

        btnDeal.setText("Deal");
        btnDeal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDealActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnDeal)
                .addContainerGap(329, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnDeal)
                .addContainerGap(262, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void makeArrayCard() {
        // Random cards
        Random rd = new Random();
        Vector v = new Vector();
        int iNew = 1;
        int iRank = 52;
        for (int i = 0; i < iRank;) {
            iNew = rd.nextInt(iRank);
            if (!v.contains(iNew)) {
                i++;
                v.add(iNew);
                //-- End Random cards

                // Add to arrJLabel
                if (i <= 13) {
                    lbl = new JLabel(new ImageIcon("D:/Card/" + (iNew + 1) + ".png"));
                } else {
                    lbl = new JLabel(new ImageIcon("D:/Card/Back.png"));
                    //lbl = new JLabel(new ImageIcon("D:/Card/" + (iNew + 1) + ".png"));
                }
                System.out.println(iCenterX + "\n" + iCenterY);
                lbl.setBounds(iCenterX, iCenterY, iCardWidth, iCardHeight);
                this.add(lbl);
                arrJLabel.add(lbl);
                //-- End Add to arrJLabel
            }
        }
    }

    @SuppressWarnings("ResultOfObjectAllocationIgnored")
    private void btnDealActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDealActionPerformed

        ArrayList<ThreadImages> arr = new ArrayList<>();
        // Begin all
        int iBeginX = iCenterX, iBeginY = iCenterY;
        //--

        // End bottom
        int iDeltaWidth = iCardWidth / 3;
        int iDeltaHeight = iCardHeight / 4;
        int iEndXBottom = (int) (iCenterX + (iCardWidth + (12 * iDeltaWidth)) / 2.5);
        int iEndYBottom = iHeight - iCardHeight - iBot;
        //--
        // End top
        int iEndXTop = (int) (iCenterX - (iCardWidth + (12 * iDeltaWidth)) / 2.5);
        int iEndYTop = iTop;
        //--
        // End right
        int iEndXRight = iCenterX - iEndXTop;
        int iEndYRight = (int) (iCenterY - (iCardHeight + (12 * iDeltaHeight)) / 2.5);
        //--
        // End left
        int iEndXLeft = iCenterX + iEndXTop;
        int iEndYLeft = (int) (iCenterY + (iCardHeight + (12 * iDeltaHeight)) / 3);
        //--

        for (int i = 0; i < arrJLabel.size(); i++) {
            // Bottom
            if (i <= 12) {
                ThreadImages thCard = new ThreadImages(arrJLabel.get(i), iBeginX, iBeginY, iEndXBottom, iEndYBottom);
                arr.add(thCard);
                iEndXBottom -= iDeltaWidth;
            }
            //--
            // Left
            if (i >= 13 && i <= 25) {
                ThreadImages thCard = new ThreadImages(arrJLabel.get(i), iBeginX, iBeginY, iEndXRight, iEndYRight);
                arr.add(thCard);
                iEndYRight += iDeltaHeight;
            }
            //--
            // Top
            if (i >= 26 && i <= 38) {
                ThreadImages thCard = new ThreadImages(arrJLabel.get(i), iBeginX, iBeginY, iEndXTop, iEndYTop);
                arr.add(thCard);
                iEndXTop += iDeltaWidth;
            }
            //--
            //Right
            if (i >= 39 && i <= 51) {
                ThreadImages thCard = new ThreadImages(arrJLabel.get(i), iBeginX, iBeginY, iEndXLeft, iEndYLeft);
                arr.add(thCard);
                iEndYLeft -= iDeltaHeight;
            }
            //--
        }
        new ThreadMain(arr);
    }//GEN-LAST:event_btnDealActionPerformed

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DealCard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DealCard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DealCard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DealCard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DealCard().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDeal;
    // End of variables declaration//GEN-END:variables
}
